(function() {

	var RegionsObj = {
		'createLink'    : function (url, name) {
			return $('<a />').attr('href', url).html(name);
		},
		
		'addClick'      : function (link, has_subregions, id) {
			if (has_subregions) {
				var self = this;
				link.on('click', function (event) {
                $.get(json_cities_url, { rid: id},
                  function(data){
                    //alert("Data Loaded: " + data);
                    if (!data.error) {
                        self.addBreadcrumbElement($('#regionsLabel'), link.attr('href'), link.html());
                        self.showList(data);
                    }
                  });	
					return false;
				});
			}
		},
		
		'createRegion'  : function (region) {
			var link = this.createLink(region.url, region.name);
			this.addClick(link, region.has_subregions, region.id);
			
			return $('<p>').append(link);
		},
		
		'clearList'     : function () {
			$('.region_cols').html('');
		},
		
		'showList'      : function (regions) {
			this.clearList();
			
			for(var key in regions) {
				this.createRegion(regions[key]).appendTo('#regions_col_'+(key % 3));
			}
		},
		
		'createBreadcrumbSpan'           : function (url, name) {
			return $('<span />').attr('data-href', url).html(name);
		},
		
		'createBreadcrumbLink'           : function (url, name) {
			return $('<a />').attr('href', url).html(name);
		},
		
		'addBreadcrumbClick'             : function (link) {
			var self = this;
			
			link.on('click', function (event) {
                $.get(json_regions_url,
                  function(data){
                    //alert("Data Loaded: " + data);
                    if (!data.error) {
                        self.deleteBreadcrumbElements($('#regionsLabel'), link);
                        self.showList(data);
                    }
                  });	                
				return false;
			});
		},
		
		'changeBreadcrumbElementToLink'  : function (breadcrumbOuter, obj) {
			var url = obj.data('href');
			var name = obj.html();
			
			var newObj = this.createBreadcrumbLink(url, name);
			this.addBreadcrumbClick(newObj);
			
			obj.replaceWith(newObj);
		},
		
		'changeBreadcrumbElementToSpan'  : function (breadcrumbOuter, obj) {
			var url = obj.attr('href');
			var name = obj.html();
			
			obj.replaceWith(this.createBreadcrumbSpan(url, name));
		},
		
		'addBreadcrumbElement'          : function (breadcrumbOuter, url, name) {
			this.changeBreadcrumbElementToLink(breadcrumbOuter, breadcrumbOuter.find('span:last'));
			breadcrumbOuter.append(' &rarr; ').append(this.createBreadcrumbSpan(url, name));
		},
		
		'deleteBreadcrumbElements'      : function (breadcrumbOuter, obj) {
			var self = this;
			
			var objNum = breadcrumbOuter.find('a').index(obj);
			var tempBreadcrumbOuter = $('<div />');
			
			breadcrumbOuter.find('a:lt('+objNum+')').each(function (index) {
				tempBreadcrumbOuter.append($(this)).append(' &rarr; ');
			});
			
			tempBreadcrumbOuter.append(obj);
			this.changeBreadcrumbElementToSpan(tempBreadcrumbOuter, obj);
			
			breadcrumbOuter.html(tempBreadcrumbOuter.html());
			
			breadcrumbOuter.find('a').each(function (index) {
				self.addBreadcrumbClick($(this));
			});
		}
	}

	// $('#myModal').on('show', function () {
		// RegionsObj.showList(regions);
	// });
	// $('#myModal').modal({'keyboard' : true,  'show' : flag_show_RegionsPopup});

  var regionsModalTrigger = $(".region-modal");
  var regionsModal = $(regionsModalTrigger.data('target'));
console.log(regionsModalTrigger);
console.log(regionsModal);

  if (regionsModalTrigger.length && regionsModal.length) {
//    regionsModal.html(RegionsObj.showList(regions));
    //RegionsObj.showList(regions);
    regionsModalTrigger.on('click', function(el){
      RegionsObj.showList(regions);
    });
    regionsModalTrigger.leanModal({
      closeButton: ".close-link"
    });
  }
    
	// $('body').append('<script src="/js/bw/bw.js"></script>');
	// browserWarning('/js/bw');
  
  
  
}());